package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.NoticeUser;
import com.xmtlxx.system.domain.bo.NoticeUserBo;
import com.xmtlxx.system.domain.vo.NoticeUserVo;
import com.xmtlxx.system.mapper.NoticeUserMapper;
import com.xmtlxx.system.service.INoticeUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 公告用户Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-05-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NoticeUserServiceImpl implements INoticeUserService {

    private final NoticeUserMapper baseMapper;

    /**
     * 查询公告用户
     *
     * @param noticeUserId 公告用户主键
     * @return 公告用户
     */
    @Override
    public NoticeUserVo queryById(Integer noticeUserId){
        return baseMapper.selectVoById(noticeUserId);
    }

    /**
     * 查询公告用户列表
     *
     * @param vo 公告用户
     * @return 公告用户
     */
    @Override
    public TableDataInfo<NoticeUserVo> queryPageList(NoticeUserVo vo, PageQuery pageQuery) {
//        LambdaQueryWrapper<NoticeUser> lqw = buildQueryWrapper(bo);
        Page<NoticeUserVo> result = baseMapper.selectPageNoticeUserList(pageQuery.build(),vo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公告用户列表
     *
     * @param bo 公告用户
     * @return 公告用户
     */
    @Override
    public List<NoticeUserVo> queryList(NoticeUserBo bo) {
        LambdaQueryWrapper<NoticeUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NoticeUser> buildQueryWrapper(NoticeUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NoticeUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getNoticeId() != null, NoticeUser::getNoticeId, bo.getNoticeId());
        lqw.eq(bo.getUserId() != null, NoticeUser::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), NoticeUser::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增公告用户
     *
     * @param bo 公告用户
     * @return 结果
     */
    @Override
    public Boolean insertByBo(NoticeUserBo bo) {
        NoticeUser add = BeanUtil.toBean(bo, NoticeUser.class);
        validEntityBeforeSave(add);

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNoticeUserId(add.getNoticeUserId());
        }
        return flag;
    }

    /**
     * 修改公告用户
     *
     * @param bo 公告用户
     * @return 结果
     */
    @Override
    public Boolean updateByBo(NoticeUserBo bo) {
        NoticeUser update = BeanUtil.toBean(bo, NoticeUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(NoticeUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公告用户
     *
     * @param ids 需要删除的公告用户主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isDel) {
        if(ids != null && ids.size() > 0){
            if(isDel){
                baseMapper.deleteBatchIds(ids);
            }else{
                for(Long id : ids){
                    NoticeUser enty = baseMapper.selectById(id);
                    enty.setStatus("1");
                    baseMapper.updateById(enty);
                }
            }
        }
        return true;
    }
}
